﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace auxiliary_proj
{
    public partial class UCViewVendor : UserControl
    {
        public UCViewVendor()
        {
            InitializeComponent();
            this.dataGridVendors.CellContentDoubleClick +=new DataGridViewCellEventHandler(dataGridVendors_CellContentDoubleClick);
            dataGridVendors.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
            viewVendors();
        }

        public void viewVendors()
        {
            string query = "SELECT vendor_id, name, address, location, phone FROM vendor";
            try
            {
                //bind data to grid
                dataGridVendors.DataSource = Query.select(query);
            }
            catch (MySqlException exc) { MessageBox.Show(exc.Message); }
        }

        private void dataGridVendors_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            FormVendorUpdate exists = Application.OpenForms.OfType<FormVendorUpdate>().FirstOrDefault();
            if (exists == null)
            {
                FormVendorUpdate fvu = new FormVendorUpdate(dataGridVendors.Rows[e.RowIndex].Cells["vendor_id"].Value.ToString(), dataGridVendors.Rows[e.RowIndex].Cells["name"].Value.ToString(), dataGridVendors.Rows[e.RowIndex].Cells["address"].Value.ToString(), dataGridVendors.Rows[e.RowIndex].Cells["location"].Value.ToString(), dataGridVendors.Rows[e.RowIndex].Cells["phone"].Value.ToString());
                fvu.StartPosition = FormStartPosition.CenterScreen;
                fvu.Show();
            }
            else
            {
                exists.Close();
                exists.Dispose();
                FormVendorUpdate fvu = new FormVendorUpdate(dataGridVendors.Rows[e.RowIndex].Cells["vendor_id"].Value.ToString(), dataGridVendors.Rows[e.RowIndex].Cells["name"].Value.ToString(), dataGridVendors.Rows[e.RowIndex].Cells["address"].Value.ToString(), dataGridVendors.Rows[e.RowIndex].Cells["location"].Value.ToString(), dataGridVendors.Rows[e.RowIndex].Cells["phone"].Value.ToString());
                fvu.StartPosition = FormStartPosition.CenterScreen;
                fvu.Show();
            }
        }
    }
}
